﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ditto.Helpers;
using Ditto.Models;

namespace Ditto.Controllers
{

    [HandleErrorWithELMAH]
    public class CommentsController : Controller
    {
        ICommentRepository commentRepository;

        public CommentsController() : this(new CommentRepository()) { }

        public CommentsController(ICommentRepository repository)
        {
            this.commentRepository = repository;
        }

        public ActionResult Item(int itemID)
        {
            var comments = this.commentRepository.FindItemComments(itemID);
            return View("Comments", new Comments(comments.ToList(), Comment.CommentType.Item, itemID));
        }
        
        public ActionResult User(string userID)
        {
            var comments = this.commentRepository.FindUserComments(userID);
            return View("Comments", new Comments(comments.ToList(), Comment.CommentType.User, userID));
        }

        public ActionResult Transaction(int transactionID)
        {
            var comments = this.commentRepository.FindTransactionComments(transactionID);
            return View("Comments", new Comments(comments.ToList(), Comment.CommentType.Transaction, transactionID));
        }

        public ActionResult New(Comment.CommentType type, object subjectID)
        {
            Comment newComment = new Comment(type, subjectID);
            return View("Comment", newComment);
        }

        public ActionResult Feedback(Comment.CommentType type, object subjectID)
        {
            Comment newComment = new Comment(type, subjectID);
            return View("Feedback", newComment);
        }

        [HttpPost, Authorize]
        public ActionResult New(Comment.CommentType type, object subjectID, string message, double? rating)
        {
            Comment comment = new Comment();

            comment.Type = type;
            comment.SubjectID = ((object[])subjectID)[0];
            comment.By = base.User.Identity.Name;
            comment.Time = DateTime.UtcNow;

            comment.Message = message;
            comment.Rating = rating;

            switch (comment.Type)
            {
                case Comment.CommentType.Item:
                    try
                    {
                        this.commentRepository.AddItemComment(comment);
                        this.commentRepository.Save();

                        return RedirectToAction("Item", new { itemID = comment.SubjectID });
                    }
                    catch
                    {
                        return RedirectToAction("Item", new { itemID = comment.SubjectID });
                    }

                case Comment.CommentType.User:
                    try
                    {
                        this.commentRepository.AddUserComment(comment);
                        this.commentRepository.Save();

                        return RedirectToAction("User", new { userID = comment.SubjectID });
                    }
                    catch
                    {
                        return RedirectToAction("User", new { userID = comment.SubjectID });
                    }

                case Comment.CommentType.Transaction:
                    try
                    {
                        this.commentRepository.AddTransactionComment(comment);
                        this.commentRepository.Save();

                        return RedirectToAction("Transaction", new { transactionID = comment.SubjectID });
                    }
                    catch
                    {
                        return RedirectToAction("Transaction", new { transactionID = comment.SubjectID });
                    }

                default:
                    throw new Exception();
            }
        } 

        public ActionResult Delete(int id, Comment.CommentType type)
        {
            try
            {
                this.commentRepository.DeleteComment(id, type);
                this.commentRepository.Save();

                //if (Request.IsAjaxRequest())
                    return Content("<li></li>");
               // else
                    //return Redirect(returnUrl);
                   // throw new Exception();
            }
            catch
            {
                throw new Exception();
                //return Redirect(returnUrl);
            }
        }
    }
}
